from . import serializers
from . import models

from rest_framework.generics import ListAPIView
from django.core.cache import cache
from django.conf import settings
from utilss.response import APIResponse

from rest_framework.views import APIView


class BannerListAPIView(ListAPIView):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by("-order")[
               :settings.BANNER_COUNT]

    serializer_class = serializers.BannerModelSerializer

    def get(self, request, *args, **kwargs):
        # 从缓存中获取数据
        banner_list = cache.get("banner_list")

        # 缓存不存在在自己查找，保存到缓存中
        if not banner_list:
            # print("走进数据库")
            banner_list = self.list(request, *args, **kwargs)

            cache.set("banner_list", banner_list.data)
        # print(banner_list)

        return APIResponse(results=banner_list)


class LoveAPIView(APIView):

    def get(self, request, *args, **kwargs):
        return APIResponse(results='http://127.0.0.1:8000/media/Love.html')
